package com.wy.seekbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:8/3/21
 * Author:ypf
 * Description:
 */
public class SeekBar extends View {


    private String TAG = "SeekBar";

    private List<String> mAbcList;

    private int mAbcPositon = 0;

    private Paint mAbcPaint;

    private Paint mAbcBluePaint;

    public interface AbcSelect {
        void select(String abc);
    }

    private AbcSelect mAbcSelect;

    public SeekBar(Context context) {
        this(context, null, 0);
    }

    public SeekBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        //每一个字母框的高度
        int perHeight = height / 26;
        Log.i(TAG, "perHeight:" + perHeight);

        for (int i = 0; i < mAbcList.size(); i++) {

            //每个字母，要加上之前的已占用的高度
            int iPerHeight = perHeight * i;

            //当前字母
            String abc = mAbcList.get(i);

            //计算基线的坐标
            Paint.FontMetricsInt fontMetrics = mAbcPaint.getFontMetricsInt();
            int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
            //之前字母已经用的高度+基线的高度，才是每个基线的高度
            int baseLine = perHeight / 2 + iPerHeight + dy;

            //计算文字宽度
            Rect bounds = new Rect();
            mAbcPaint.getTextBounds(abc, 0, abc.length(), bounds);

            //文字的x坐标
            int drawTextX = width / 2 - bounds.width() / 2;

            //处理选中蓝色
            if (i == mAbcPositon) {
                //画字母
                canvas.drawText(abc, drawTextX, baseLine, mAbcBluePaint);
            } else {
                //画字母
                canvas.drawText(abc, drawTextX, baseLine, mAbcPaint);
            }

        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int height = getHeight();
        //每一个字母框的高度
        int perHeight = height / 26;

        float y = event.getY();

        mAbcPositon = (int) (y / perHeight);

        if (null != mAbcSelect) {
            mAbcSelect.select(mAbcList.get(mAbcPositon));
        }

        Log.i(TAG, "abc:" + mAbcList.get(mAbcPositon));

        invalidate();

        return super.onTouchEvent(event);
    }

    public void setSelectLisentering(AbcSelect abcSelect) {
        mAbcSelect = abcSelect;
    }


    private void init() {

        mAbcList = new ArrayList<>();
        mAbcList.add("A");
        mAbcList.add("B");
        mAbcList.add("C");
        mAbcList.add("D");
        mAbcList.add("E");
        mAbcList.add("F");
        mAbcList.add("G");
        mAbcList.add("H");
        mAbcList.add("I");
        mAbcList.add("J");
        mAbcList.add("K");
        mAbcList.add("L");
        mAbcList.add("M");
        mAbcList.add("N");
        mAbcList.add("O");
        mAbcList.add("P");
        mAbcList.add("Q");
        mAbcList.add("R");
        mAbcList.add("S");
        mAbcList.add("T");
        mAbcList.add("U");
        mAbcList.add("V");
        mAbcList.add("W");
        mAbcList.add("X");
        mAbcList.add("Y");
        mAbcList.add("Z");

        mAbcPaint = new Paint();
        mAbcPaint.setColor(Color.BLACK);
        mAbcPaint.setTextSize(50);

        mAbcBluePaint = new Paint();
        mAbcBluePaint.setColor(Color.BLUE);
        mAbcBluePaint.setTextSize(50);
        mAbcBluePaint.setTypeface(Typeface.DEFAULT_BOLD);


    }

}
